#include "Skeleton.h"
#include "DistanceTransform.h"



Skeleton::Skeleton()
{

}



Ptr< QImage > Skeleton::process (Ptr< QImage > pSrcImage)
{
	Ptr< QImage > pDestImage = new QImage (pSrcImage->size (),
			QImage::Format_ARGB32);
	pDestImage->fill (qRgba (0, 0, 0, 255));
	Ptr< QImage > pTempImage = DistanceTransform ().process (pSrcImage);
	for (int v = 1; v < pTempImage->height () - 1; ++v) {
		QRgb * destLine = (QRgb *)pDestImage->scanLine (v);
		QRgb * tempLine = (QRgb *)pTempImage->scanLine (v);
		QRgb * tempPrevLine = (QRgb *)pTempImage->scanLine (v - 1);
		QRgb * tempNextLine = (QRgb *)pTempImage->scanLine (v + 1);
		for (int u = 1; u < pTempImage->width () - 1; ++u) {
			int val = qRed (tempLine[u]);
			if (val != 0) {
				int valLeft = qRed (tempLine[u - 1]);
				int valRight = qRed (tempLine[u + 1]);
				int valTop = qRed (tempPrevLine[u]);
				int valTopLeft = qRed (tempPrevLine[u - 1]);
				int valTopRight = qRed (tempPrevLine[u + 1]);
				int valDown = qRed (tempNextLine[u]);
				int valDownLeft = qRed (tempNextLine[u - 1]);
				int valDownRight = qRed (tempNextLine[u + 1]);
				if (val >= valLeft && val >= valRight &&
						val >= valTopLeft && val >= valTop && val >= valTopRight && 
						val >= valDownLeft && val >= valDown && val >= valDownRight)
					destLine[u] = qRgba (255, 255, 255, 255);
			}
		}
	}
	return pDestImage;
}
